package com.sl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sl.entity.PageResult;
import com.sl.entity.Result;
import com.sl.entity.StatusCode;
import com.sl.pojo.Company;
import com.sl.pojo.User;
import com.sl.pojo.UserCompanyPosition;
import com.sl.service.CompanyService;
import com.sl.service.UserCompanyPositionService;
import com.sl.service.UserService;
import com.sl.util.IdWorker;
import com.sl.vo.PositionVO;
import com.sl.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/position")
public class UserCompanyPositionController {

    @Autowired
    private UserCompanyPositionService userCompanyPositionService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;

    @Autowired
    private IdWorker idWorker;

    @PostMapping("/add")
    public Result add(@RequestBody String data) {
        log.info("创建位置信息>>" + data);
        UserCompanyPosition userCompanyPosition = JSONObject.parseObject(data, UserCompanyPosition.class);
        Integer userId = userCompanyPosition.getUserId();
        if (null == userId) {
            throw new RuntimeException("userId参数有误");
        }
        Integer companyId = userCompanyPosition.getCompanyId();
        if (null == companyId) {
            throw new RuntimeException("companyId参数有误");
        }
        String position = userCompanyPosition.getPosition();
        if (StringUtils.isEmpty(position)) {
            throw new RuntimeException("position参数有误");
        }
        String positionUuid = String.valueOf(idWorker.nextId());
        userCompanyPosition.setPositionId(positionUuid);
        userCompanyPositionService.addPosition(userCompanyPosition);
        return new Result(true, StatusCode.OK, "添加成功",positionUuid);
    }

    @PostMapping("/updatePosition")
    public Result update(@RequestBody String data) {
        log.info("增加位置信息" + data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String positionId = jsonObject.getString("positionId");
        if (StringUtils.isEmpty(positionId)) {
            throw new RuntimeException("id参数有误");
        }
        String position = jsonObject.getString("position");
        if (StringUtils.isEmpty(position)) {
            throw new RuntimeException("position参数有误");
        }
        log.info("positionId>>" + positionId + "::position>>>" + position);

        UserCompanyPosition positionByUUId = userCompanyPositionService.findByUUID(positionId);
        log.info("positionById>>>" + positionByUUId);
        String positionBefore = positionByUUId.getPosition();
        String substring = positionBefore.substring(0, positionBefore.length() - 1) + "," + position + "]";
        log.info("substring>>>>>" + substring);
        positionByUUId.setPosition(substring);
        userCompanyPositionService.updatePosition(positionByUUId);
        return new Result(true, StatusCode.OK, "每隔几秒更新已有位置", substring);
    }

    @PostMapping("/search")
    public Result findList(@RequestBody String data){
        log.info("searchMap>>>>>" + data );
        JSONObject jsonObject = JSON.parseObject(data);
        String dataMap = jsonObject.getString("data");
        Map map = JSON.parseObject(dataMap);
        System.out.println("map>>" +map);
        Integer page = jsonObject.getInteger("page");
        if (null ==page) {page=1;}
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (null ==pageSize) {pageSize = 10;}

        Page<UserCompanyPosition> listPotion = userCompanyPositionService.findListPotion(map, page, pageSize);

        List<PositionVO> positionVOList=new ArrayList<>();
        for (UserCompanyPosition userCompanyPosition : listPotion) {
            PositionVO positionVO = userPosition2VO(userCompanyPosition);
            positionVOList.add(positionVO);
        }
        PageResult<PositionVO> positionVOListPageResult = new PageResult<>(listPotion.getTotal(), positionVOList);
        return new Result<>(true, StatusCode.OK,"查询路线成功",positionVOListPageResult);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody String data){
        log.info("删除唯一ID为>>>>>" + data );
        JSONObject jsonObject = JSON.parseObject(data);
        String positionId = jsonObject.getString("positionId");
        UserCompanyPosition positionServiceByUUID = userCompanyPositionService.findByUUID(positionId);
        userCompanyPositionService.deleteByIdPosition(positionServiceByUUID);
        return new Result<>(true, StatusCode.OK,"删除成功");
    }

    /**
     * 用来测试
     * @return
     */
/*    @GetMapping("/all")
    public Result all() {
        log.info("查看所有位置信息");
        List<UserCompanyPosition> allPosition = userCompanyPositionService.findAllPosition();
        List<PositionVO> positionVOS = new ArrayList<>();
        for (UserCompanyPosition userCompanyPosition : allPosition) {
            PositionVO positionVO = userPosition2VO(userCompanyPosition);
            positionVOS.add(positionVO);
        }
        return new Result(true, StatusCode.OK, "查看所有位置，仅仅做测试，不能用，没分页会奔溃", positionVOS);
    }*/


    private PositionVO userPosition2VO(UserCompanyPosition userCompanyPosition) {
        log.info("用户，公司，位置信息转化");
        String string = JSON.toJSONString(userCompanyPosition);
        JSONObject jsonObject = JSONObject.parseObject(string);
        Integer userId = jsonObject.getInteger("userId");
        Integer companyId = jsonObject.getInteger("companyId");
        System.out.println("userId>>" + userId + ">>>companyId>>>" + companyId);
        JSONArray position = jsonObject.getJSONArray("position");
        List<String> list = new ArrayList<>();
        for (Object o : position) {
            list.add(o.toString());
        }
        // List<实体类或其他泛型> list = JSON.parseObject(json, new TypeReference<List<实体类或其他泛型>>(){});
        PositionVO positionVO = new PositionVO();

        User byId = userService.findById(userId);
        positionVO.setPositionId(userCompanyPosition.getPositionId());
        positionVO.setUserName(byId.getName());
        Company compangy = companyService.findById(companyId);
        positionVO.setCompanyName(compangy.getCompanyName());
        positionVO.setPosition(list);

        return positionVO;
    }




}
